package br.here.data;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.here.data.IUsuarioDAO;
import br.here.dominio.Amigo;
import br.here.dominio.Local;
import br.here.dominio.Usuario;
import br.here.exceptions.BDException;
import br.here.utils.HibernateUtil;

public class UsuarioDAOImpl extends GenericDAOImpl<Usuario> implements Serializable{

	@SuppressWarnings("unchecked")
	public Collection<Usuario> listAll() throws BDException {
		
		try {
			Session session =  HibernateUtil.getCurrentSession();
			Transaction tx = session.beginTransaction();
			Criteria c = session.createCriteria(Usuario.class);
			tx.commit();
			return c.list();
		} catch (Throwable e){
			throw new BDException();
		}
	}

	public Usuario findByEmail(String email) throws BDException {
		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = session.beginTransaction();
		Query q = session.createQuery("select u from Usuario as u where u.email='"+email+"'");
		Usuario u = (Usuario) q.uniqueResult();
		tx.commit();
		return u;
	}

	public void addUsuario(Usuario usuario) throws BDException {
		try{
			add(usuario);
		} catch (Exception e){
			throw new BDException();
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Usuario> findByNomeOuEmail(String txt) throws BDException {
		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = session.beginTransaction();
		Query q = session.createQuery("select u from Usuario as u where u.nome like '%"+txt+"%' " +
				"or u.email like '%"+txt+"%' ");
		Collection<Usuario> c =  q.list();
		tx.commit();
		return c;
	}
	
	public void addAmigo(int idU, int idA ) throws BDException{
		try{
			Session session = HibernateUtil.getCurrentSession();
			Transaction tx = session.beginTransaction();
			//Query q = session.createQuery("insert into Amigo (usuarioTo.id, usuarioFrom.id) values ("+idA+", "+idU+") ");
			Amigo a = new Amigo();
			Usuario uFrom = (Usuario)session.get(Usuario.class, idU);
			Usuario uTo = (Usuario)session.get(Usuario.class, idA);
			a.setUsuarioFrom(uFrom);
			a.setUsuarioTo(uTo);
			session.save(a);
			tx.commit();
		} catch (Exception e){
			throw new BDException();
		}
		
	}
	
	public Usuario findById(Integer id) throws BDException {
		Usuario u = findByPrimaryKey(id);
		if(u == null){
			throw new BDException();
		}else{
			return u;
		}
	}
	
	public void removeUsuarioById(Usuario u) throws BDException{
		try{
			if(u == null){
				throw new BDException();
			}
			remove(u);
		}catch(Exception e){
			throw new BDException();
		}
	}

	public void atualizaLocal(Usuario u, Local l) throws BDException {
		Session session = HibernateUtil.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try {
			u.setLocalAtual(l);
			session.update(u);
			tx.commit();
		}catch (Exception e){
			tx.rollback();
			e.printStackTrace();
			throw new BDException();
		}
		
	}

}
